package com.atguigu.gulimall.cart.controller;

import com.atguigu.common.constant.AuthServiceConstant;
import com.atguigu.gulimall.cart.feign.ProductFeignService;
import com.atguigu.gulimall.cart.interceptor.CartInterceptor;
import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.to.UserInfoTo;
import com.atguigu.gulimall.cart.vo.CartItemVo;
import com.atguigu.gulimall.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    CartService cartService;


    /**
     * 获取当前用户的购物车信息
     * @return
     */
    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItemVo> currentUserCartItems(){
        return cartService.getUserCartItems();
    }



    /**
     * 获取当前用户的购物车信息（临时购物车/用户购物车）,并跳转到购物车页面
     *
     * 浏览器设置一个cookie: user-key ， 标识临时购物车，一段时间后过期。
     * 第一次访问购物车信息，都会分配一个临时购物车，以后访问都会带上这个cookie
     *
     * 已登录：从session中获取购物车信息
     * 未登录：从cookie中获取，第一次登陆，如果没有临时购物车，则创建一个cookie
     *  用拦截器来实现：判断登录功能、分配临时购物车cookie
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {

        CartVo cartVo  = cartService.getCart();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * @param skuId 商品id
     * @param num 商品数量
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num,
                            RedirectAttributes re) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId,num);
        re.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addToSuccessPage.html";
    }

    /**
     * 跳转到商品添加成功页
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToSuccessPage.html")
    public String addToSuccessPage(@RequestParam("skuId")Long skuId,Model model){
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("item",cartItemVo);
        return "success";
    }


    /**
     * 修改某商品的选中状态
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId")Long skuId,@RequestParam("check")Integer check){
        cartService.checkItem(skuId,check);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 修改购物项的商品数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId")Long skuId,@RequestParam("num")Integer num){
        cartService.countItem(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 删除某个购物项
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("deleteId")Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

}
